// Ionic Starter App

// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
// 'starter.services' is found in services.js
// 'starter.controllers' is found in controllers.js
var app =angular.module('starter', ['ionic', 'starter.controllers', 'starter.services'])

.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {
    // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
    // for form inputs)
    if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
      cordova.plugins.Keyboard.disableScroll(true);

    }
    if (window.StatusBar) {
      // org.apache.cordova.statusbar required
      StatusBar.styleDefault();
    }
  });
})

.config(function($stateProvider, $urlRouterProvider,$ionicConfigProvider) {
  //设置 android 底部居中
  $ionicConfigProvider.platform.android.tabs.position('bottom');
  // Ionic uses AngularUI Router which uses the concept of states
  // Learn more here: https://github.com/angular-ui/ui-router
  // Set up the various states which the app can be in.
  // Each state's controller can be found in controllers.js

  $stateProvider

  // setup an abstract state for the tabs directive
    .state('tab', {
    url: '/tab',
    abstract: true,
    templateUrl: 'templates/tabs.html'
  })

  // Each tab has its own nav history stack:

      //登录
      .state('login', {
        url: '/login',
        templateUrl: 'templates/index/login.html',
        controller: 'LoginCtrl'
      })

      //注册
      .state('reg', {
        url: '/reg',
        templateUrl: 'templates/index/reg.html',
        controller: 'RegCtrl'
      })
      //忘记密码
      .state('forget', {
        url: '/forget',
        templateUrl: 'templates/index/forget.html',
        controller: 'ForgetCtrl'
      })
      //首页路由
      .state('tab.index', {
        url: '/index',
        views: {
          'tab-index': {
            templateUrl: 'templates/index/index.html',
            controller: 'IndexCtrl'
          }
        }
      })

      //商品列表
      .state('tab.productclassify', {
        url: '/product/classify/:id',
        views: {
          'tab-index': {
            templateUrl: 'templates/product/classify.html',
            controller: 'ProductClassifyCtrl'
          }
        }
      })

      //商品详情
      .state('tab.productdetail', {
        url: '/product/detail/:id',
        views: {
          'tab-index': {
            templateUrl: 'templates/product/detail.html',
            controller: 'ProductDetailCtrl'
          }
        }
      })

      //商品查询
      .state('tab.productsearch', {
        url: '/product/search',
        views: {
          'tab-search': {
            templateUrl: 'templates/product/search.html',
            controller: 'ProductSearchCtrl'
          }
        }
      })

     //商品查询列表
      .state('tab.productsearchclassify', {
        url: '/product/searchclassify/:id',
        views: {
          'tab-search': {
            templateUrl: 'templates/product/classify.html',
            controller: 'ProductClassifyCtrl'
          }
        }
      })


      //商品查询详情
      .state('tab.productsearchdetail', {
        url: '/product/searchdetail/:id',
        views: {
          'tab-search': {
            templateUrl: 'templates/product/detail.html',
            controller: 'ProductDetailCtrl'
          }
        }
      })


      //购物车
      .state('tab.cart', {
        url: '/cart',
        views: {
          'tab-cart': {
            templateUrl: 'templates/cart/cart.html',
            controller:'CartCtrl'
          }
        }
      })

    //订单页面
      .state('tab.order', {
        url: '/order',
        views: {
          'tab-cart': {
            templateUrl: 'templates/cart/order.html',
            controller:'OrderCtrl'
          }
        }
      })

      //个人中心首页主页
      .state('tab.center', {
        url: '/center',
        views: {
          'tab-center': {
            templateUrl: 'templates/center/index.html',
            controller:'CenterIndexCtrl'
          }
        }
      })

    //我的订单
      .state('tab.centertrade', {
        url: '/center/trade',
        views: {
          'tab-center': {
            templateUrl: 'templates/center/trade.html',
            controller:'CenterTradeCtrl'
          }
        }
      })


  ////////////////
      .state('tab.dash', {
        url: '/dash',
        views: {
          'tab-dash': {
            templateUrl: 'templates/tab-dash.html',
            controller: 'DashCtrl'
          }
        }
      })

      .state('tab.chats', {
        url: '/chats',
        views: {
          'tab-chats': {
            templateUrl: 'templates/tab-chats.html',
            controller: 'ChatsCtrl'
          }
        }
      })
    .state('tab.chat-detail', {
      url: '/chats/:chatId',
      views: {
        'tab-chats': {
          templateUrl: 'templates/chat-detail.html',
          controller: 'ChatDetailCtrl'
        }
      }
    })

    .state('tab.account', {
      url: '/account',
      views: {
        'tab-account': {
          templateUrl: 'templates/tab-account.html',
          controller: 'AccountCtrl'
        }
      }
  });

  //if none of the above states are matched, use this as the fallback
  $urlRouterProvider.otherwise('/login');

});


/******加入底部 tabs 隐藏标识******/
app.directive('hideTabs', function($rootScope,$location) {
  return {
    restrict: 'A',
    link: function(scope, element, attributes) {
      scope.$on('$ionicView.beforeEnter', function() {
        scope.$watch(attributes.hideTabs, function(value){
           //设置隐藏
          $rootScope.hideTabs = value;
        });
      });

      scope.$on('$ionicView.beforeLeave', function() {

        var path =$location.path();
        //alert(path);
        if(path =="/tab/index"){
          $rootScope.hideTabs = false;
        }
      });
    }
  };
});